/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.swing.filechooser;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.filechooser.FileFilter;
import ca.forklabs.baselib.swing.filechooser.AbstractFileFilter;
import ca.forklabs.baselib.util.Algorithm;
import ca.forklabs.baselib.util.UnaryFunction;
import ca.forklabs.baselib.util.UnaryPredicate;

/**
 * Class {@link CompositeFileFilter} is the union of multiple file filters.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.swing.filechooser.CompositeFileFilter">Daniel Léonard</a>
 * @version $Revision$
 */
public class CompositeFileFilter extends AbstractFileFilter {

//---------------------------
// Instance variable
//---------------------------

   /** The list of filters. */
   private List<FileFilter> the_filters = new LinkedList<>();


//---------------------------
// Constructor
//---------------------------

    /**
     * Constructor.
     * @param   description   the description.
     */
    public CompositeFileFilter(String description) {
       this(description, new FileFilter[0]);
       }

    /**
     * Constructor.
     * @param   description   the description.
     * @param   filters   the initial filters.
     */
    public CompositeFileFilter(String description, FileFilter...filters) {
       this(description, Arrays.asList(filters));
       }

    /**
     * Constructor.
     * @param   description   the description.
     * @param   filters   the initial filters.
     */
    public CompositeFileFilter(String description, Collection<FileFilter> filters) {
       super(description);
       this.addFilters(filters);
       }


//---------------------------
// Accessor
//---------------------------

   /**
    * Gets the filters.
    * @return   the filters.
    */
   protected List<FileFilter> getFilters() {
      return this.the_filters;
      }


//---------------------------
// Instance method
//---------------------------

   /**
    * Adds a filter.
    * @param   filter   a new filter.
    */
   public void addFilter(FileFilter filter) {
      List<FileFilter> filters = this.getFilters();
      filters.add(filter);
      }

   /**
    * Adds multiple filters.
    * @param   filters   the filters.
    */
   public void addFilters(Collection<FileFilter> filters) {
      Iterator<FileFilter> iterator = filters.iterator();
      Algorithm.forEach(iterator, new UnaryFunction<Void, FileFilter>() {
         @Override
         public Void invoke(FileFilter filter) {
            CompositeFileFilter composite = CompositeFileFilter.this;
            composite.addFilter(filter);
            return null;
            }
         });
      }

   /**
    * Removes a filter.
    * @param   filter   the filter to remove.
    */
   public void removeFilter(FileFilter filter) {
      List<FileFilter> filters = this.getFilters();
      filters.remove(filter);
      }

   /**
    * Gets the iterator on the {@link FileFilter}s.
    * @return   the iterator.
    */
   protected Iterator<FileFilter> getFilterIterator() {
      List<FileFilter> filters = this.getFilters();
      Iterator<FileFilter> iterator = filters.iterator();
      return iterator;
      }


//---------------------------
// Implemented methods from javax.swing.filechooser.FileFilter
//---------------------------

  /**
   * Accepts the file if at least one filter accepts it.
   * @param   file   the file to test.
   * @return   {@code true} if at least one child filter accepts the file,
   *           {@code false} otherwise.
   */
   @Override
   public boolean accept(File file) {
      Iterator<FileFilter> iter = this.getFilterIterator();

      final File candidate = file;

      boolean acceptance = Algorithm.findIf(iter, new UnaryPredicate<FileFilter>() {
         @Override
         public boolean eval(FileFilter filter) {
            boolean evaluation = filter.accept(candidate);
            return evaluation;
            }
         });
      return acceptance;
      }

   }
